<template>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>贴子管理</el-breadcrumb-item>
        <el-breadcrumb-item>贴子列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-row :gutter="20">
          <el-col :span="7">
            <el-input placeholder="请输入内容" v-model="queryInfo.searchwords" clearable @clear="getTopicList" >
              <el-button slot="append" icon="el-icon-search" @click="searchTopic"></el-button>
            </el-input>
          </el-col>
        </el-row>
        <el-table
          :header-cell-style="centerStyle"
          :cell-style="centerStyle"
          :data="topicList"
          stripe
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            width="80"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="user.nickname"
            label="发布者"
            width="100">
          </el-table-column>
          <el-table-column
            prop="createdate"
            label="发布时间"
            width="100">
          </el-table-column>
          <el-table-column
            prop="content"
            label="内容"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="imgurl"
            label="图片"
            :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span v-if="scope.row.imgurl == null">无</span>
              <span v-else>{{scope.row.imgurl}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="200">
            <template slot-scope="scope">
              <el-button @click="showTopic(scope.row.id)">查看</el-button>
              <el-button @click="deleteTopic(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 3, 5, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
        <el-dialog
          :visible.sync="showDialogVisible"
          width="40%">
                      <div class="user_info">
              <div class="img">
                <img  :src="topicInfo.user.avatarurl" style="width: 60px; border-radius: 50%;" mode="widthFix" />
              </div>
              <div class="right">
                <div class="nickname">{{topicInfo.user.nickname}}</div>
                <div class="date">{{topicInfo.createdate}}</div>
              </div>
            </div>
          <div class="topic_info">

            <!-- 贴子内容 -->
            <div class="content">
              <div class="content_word">
                  {{topicInfo.content}}
              </div>
              <div class="content_img">
                <img :src="topicInfo.imgurl" style="width: 150px;" class="" mode="widthFix" />
              </div>
            </div>
          </div>
        </el-dialog>
      </el-card>
    </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        searchwords: '',
        pagesize: 10,
        pagenum: 1
      },
      topicList: [],
      total: 0,
      showDialogVisible: false,
      topicInfo: {
        createdate: '',
        imgurl: '',
        user: {},
        content: ''
      },
      date: ''
    }
  },
  created () {
    this.getTopicList()
  },
  methods: {
    //  获取贴子列表
    async getTopicList () {
      const res = await this.$http.get('topic/getAllTopicByPage', {
        params: {
          pageNum: this.queryInfo.pagenum,
          pageSize: this.queryInfo.pagesize
        }
      })
      if (res.status !== 200) {
        this.$message.error('获取贴子列表失败')
      } else {
        this.topicList = res.data.list
        this.total = res.data.total
      }
    },
    // 表格内容居中样式
    centerStyle () {
      return 'text-align: center'
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getTopicList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getTopicList()
    },
    // 查看贴子对话框
    async showTopic (id) {
      this.showDialogVisible = true
      const res = await this.$http.get('topic/getTopicById', { params: { id: id } })
      if (res.status !== 200) {
        this.$message.error('获取贴子信息失败')
      } else {
        this.topicInfo = res.data[0]
      }
    },
    // 删除贴子
    deleteTopic (id) {
      const name = window.sessionStorage.getItem('name')
      if (name !== null) {
        this.$confirm('此操作将永久删除该贴子, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$http.post('topic/deleteTopicById?id=' + id)
          if (res.data === 'success') {
            this.$message.success('删除成功')
            this.getTopicList()
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        this.$confirm('您还没有登录，请先登录', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$router.push('/login')
        }).catch(() => {
          return false
        })
      }
    },
    // 搜索贴子
    async searchTopic () {
      const res = await this.$http.get('topic/getTopicByWords', { params: { searchwords: this.queryInfo.searchwords } })
      if (res.status !== 200) {
        this.$message.error('查询失败')
      } else {
        this.topicList = res.data
      }
    }
  }
}
</script>

<style lang="less" scoped>
.topic_info {
 display: flex;
 flex-direction: column;
 align-items: center;
}
.user_info {
  display: flex;
  flex-direction: row;
  margin-left: 135px;
  .right {
    margin-left: 15px;
    .nickname {
      font-size: 18px;
      letter-spacing: 1px;
      margin-bottom: 12px;
    }
  }
}
.content {
  display: flex;
  flex-direction: column;
  margin-top: 15px;
  font-size: 16px;
  max-width: 200px;
  .content_img {
    margin-top: 10px;
  }
}

</style>
